<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件

function make_password($password){
    return md5('fuck' . $password . 'fuck');
}

/**
 * 以key为键名重组数组
 * @param $array
 * @param $index
 * @return mixed
 */
function array_columns($array, $key){
    $arr = [];
    $array = is_object($array) ? json_decode($array, true) : $array;
    foreach ($array as $val){
        $arr[$val[$key]] = $val;
    }
    return $arr;
}

/**
 * 获取客户端IP地址
 * @param integer $type 返回类型 0 返回IP地址 1 返回IPV4地址数字
 * @param boolean $adv 是否进行高级模式获取（有可能被伪装）
 * @return mixed
 */
function get_client_ip($type = 0,$adv=false) {
    $type       =  $type ? 1 : 0;
    static $ip  =   NULL;
    if ($ip !== NULL) return $ip[$type];
    if($adv){
        if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
            $arr    =   explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
            $pos    =   array_search('unknown',$arr);
            if(false !== $pos) unset($arr[$pos]);
            $ip     =   trim($arr[0]);
        }elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
            $ip     =   $_SERVER['HTTP_CLIENT_IP'];
        }elseif (isset($_SERVER['REMOTE_ADDR'])) {
            $ip     =   $_SERVER['REMOTE_ADDR'];
        }
    }elseif (isset($_SERVER['REMOTE_ADDR'])) {
        $ip     =   $_SERVER['REMOTE_ADDR'];
    }
    // IP地址合法验证
    $long = sprintf("%u",ip2long($ip));
    $ip   = $long ? array($ip, $long) : array('0.0.0.0', 0);
    return $ip[$type];
}

/**
 * 发送curl请求
 * @param $url
 * @param null $data
 * @param $header
 * @return mixed
 */
function send_curl($url, $data = null,$header='') {
    $ch = curl_init ();
    curl_setopt ( $ch, CURLOPT_URL, $url );
    if (!empty($data)) {
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, is_array($data) ? http_build_query($data) : $data);
    }
    if(!empty($header)){
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    }
    curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
    curl_setopt ( $ch, CURLOPT_CONNECTTIMEOUT, 10 );
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
    $handles = curl_exec ( $ch );
    $log_handles = json_decode($handles) ? json_decode($handles, true) : $handles;
    think\facade\Log::write($url . ' ====> ' . (is_array($data) ? json_encode($data, JSON_UNESCAPED_UNICODE) : $data) . ' ====> ' . (is_array($log_handles) ? print_r($log_handles, true) : $log_handles), 'curl');
    if (curl_errno($ch)) {
        think\facade\Log::write($url . ' ====>  error  ====> ' . curl_error($ch), 'curl');
    }
    curl_close ( $ch );

    return $handles;
}



/**
 * json中止返回数据
 * @param array $data
 * @param string $msg
 */
function ajax_return($data = array(), $msg= ''){
    $res['status'] = $data === false ? false : true;
    $res['data'] = $data;
    $res['msg'] = $msg ? $msg : ($data === false ? '操作失败' : '操作成功');
    exit(json_encode($res, JSON_UNESCAPED_UNICODE));
}

/**
 * 获取完整的host链接
 * @return string
 */
function get_full_host(){
    $http_type = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')) ? 'https://' : 'http://';
    return $http_type . $_SERVER["HTTP_HOST"];
}





